package cn.cau.epidemicsurveysystem.control;

import cn.cau.epidemicsurveysystem.bean.StaffInfoBean;
import cn.cau.epidemicsurveysystem.service.StaffInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserController {

    @Autowired
    StaffInfoService staffInfoService;

    /**
     * @param name:
     * @param password:
     * @param session:
     * @return ModelAndView
     * @author zy
     * @description TODO
     * @date 2022/2/28 11:11
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ModelAndView login(@RequestParam("name") String name, @RequestParam("password") String password, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        StaffInfoBean staffInfo = staffInfoService.findStaffByNo(name);
        //status字段用于指示用户的登录状态
        //0-用户名错误
        //1-密码错误
        //2-登录成功
        if(staffInfo==null){   //用户名错误
            modelAndView.addObject("status",0);
            modelAndView.setViewName("login");
        }
        else{
            if(!staffInfo.getStaffPwd().equals(password)){    //密码错误
                modelAndView.addObject("status",1);
                modelAndView.setViewName("login");
            }
            else{
                session.setAttribute("staffInfo", staffInfo);
                System.out.println(session.getAttribute("staffInfo"));
                if(staffInfo.getStaffPosition().equals("医生")){
                    response.sendRedirect(request.getContextPath()+"/reportList");
                }
                else {
                    modelAndView.setViewName("index");
                }
            }
        }
        return modelAndView;
    }
//    @RequestMapping(value = "/register", method = RequestMethod.POST)
//    public String updatePassword(String newPassWord, Model model, HttpSession session) {
//        // 错误信息
//        String error = "";
//        UserBean user = (UserBean) session.getAttribute("userInfo");
//        boolean isSuccess = staffInfoService.modifyPassword(user.getUsername(), newPassWord);
//        if (isSuccess) {
//            error = "密码修改成功";
////            log.info(error);
//            model.addAttribute("error", error);
//            return "user/login";
//        } else {
//            error = "密码修改失败";
////            log.info(error);
//            model.addAttribute("error", error);
//            return "user/updatePassword";
//        }
//    }
}
